package common.start;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/**
 *
 *
 * CreateBy: <a href="wangjunneil@gmail.com">Calvin</a>
 * CreateTime: 2013-12-06 16:45
 */
public class DaemonServer {

    /**
     * 日志对象引用
     */
    private static final Log log = LogFactory.getLog(DaemonServer.class);

    private boolean isRunning = true;

    private BootConfig bootConfig;

    public DaemonServer (BootConfig bootConfig) {
        this.bootConfig = bootConfig;
    }

    public void start() throws Exception {
        ServerSocket serverSocket = new ServerSocket(bootConfig.getDaemonPort());
        Socket socket = null;
        BufferedReader br = null;
        while (isRunning) {
            socket = serverSocket.accept();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            if ("stop".equals(br.readLine())) break;
//            if ("GET / HTTP/1.1".equals(br.readLine())) httpProcess(socket);
        }
        serverSocket.close();

        log.info("system is closed.");
        System.exit(0);
    }

    public void stop() throws Exception {
        Socket socket = new Socket("127.0.0.1", bootConfig.getDaemonPort());
        PrintWriter writer = new PrintWriter(socket.getOutputStream());
        writer.println("stop");
        writer.close();
        socket.close();
    }

//    private void httpProcess(Socket socket) throws Exception {
//        PrintWriter writer = new PrintWriter(socket.getOutputStream());
//
//        StringBuffer body = new StringBuffer();
//        body.append("<html>")
//            .append("<head><title>Daemon Server Status</title></head>")
//            .append("<body>")
//                .append("<h3 style=\"font-family: Verdana, Helvetica, sans-serif;background-color: #294563;color: #ffffff;font-weight: bold;padding: 2px;margin-top: 20px;clear: right;\">")
//                .append("The Daemon Is Running ...")
//                .append("</h3>")
//                .append("<p style=\"font-family: arial,helvetica,sanserif;font-size: 70%;background-color: #CFDCED;clear: both;text-align: right;margin: 0px;border-top: thin solid #4C6C8F;\">")
//                .append("Copyright &copy; 2010-2012 <a href=\"http://www.5ifilm.cn\">5ifilm</a>. Allrights reserved.")
//                .append("Create by <a href=\"mailto:wangjunneil@gmail.com\">wangjunneil@gmail.com</a>")
//                .append("</p>")
//            .append("</body>")
//            .append("</html>");
//
//        StringBuffer header = new StringBuffer();
//        header.append("HTTP/1.1 200 OK").append("\r\n")
//                .append("Date:").append(new Date()).append("\r\n")
//                .append("Server:").append("DaemonServer").append("\r\n")
//                .append("Content-type:text/html ").append("\r\n")
//                .append("Content-length:").append(body.length()).append("\r\n\r\n");
//        header.append(body.toString());
//
//        writer.println(header.toString());
//        writer.close();
//        socket.close();
//    }


}
